  \redish{//lock-free barrier function}
   __device__ \greenish{void} \bluish{__gpu_sync}(\greenish{int} \brownish{goalVal}, \greenish{int} \brownish{*Ain},
                                \greenish{int} \brownish{*Aout})\{

      \greenish{int} \brownish{tid_in_block} = threadIdx.x * blockDim.y + 
            threadIdx.y;
      \greenish{int} \brownish{nBlockNum} = gridDim.x * gridDim.y; 
      \redish{// each thread of the first block is }
      \redish{// associated to a block it monitors}
      \greenish{int} \brownish{bid} = blockIdx.x * gridDim.y + blockIdx.y;

      \redish{// thread 0 of each block states its arrival}
      \purplish{if} (tid_in_block == 0)\{  
          Ain[bid] = goalVal;  
       \}

      \purplish{if} (bid == 1)\{ 
          \redish{// threads in the first block wait for }
          \redish{// the associated block}
          \purplish{if} (tid_in_block < nBlockNum) \{
             \purplish{while}(Ain[tid_in_block] != goalVal)\{ \}
          \}

	  \redish{// synchronize threads of first block}
	   __synchthreads();

  	  \redish{// release associated block}
          \purplish{if} (tid_in_block < nBlockNum) \{
              Aout[tid_in_block] = goalVal;
          \}
      \}
      \redish{// each block waits to be released}
      \purplish{if} (tid_in_block == 0) \{
          \purplish{while}(Aout[tid_in_block] != goalVal) \{ \}
      \}

      \redish{// synchronize within the block}
        __synchthreads();
  \}
